package com.youaishujuhui.devops.web.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gzyouai.hummingbird.common2.util.StringUtil;
import com.youaishujuhui.devops.web.domain.manage.SysUser;
import com.youaishujuhui.devops.web.util.WebUtil;

@Controller
@RequestMapping("/")
public class IndexController {
	
    private Logger logger = LoggerFactory.getLogger(getClass());
    
    private String defaultRedirect = "manage/page";

	@RequestMapping("/")
	public ModelAndView index(String redirect,Integer productId, HttpServletRequest request, String backUrl){
		
		ModelAndView modelAndView = new ModelAndView("/index");

		if(StringUtil.isNullOrEmpty(redirect)){
			String contextPath = request.getContextPath();
			redirect = contextPath + defaultRedirect;
		}
		
		SysUser u = WebUtil.getLoginUser(request);
		if (u != null) {
			modelAndView.addObject("userName", u.getUserName());
		}
		
		modelAndView.addObject("redirect", defaultRedirect);

		//默认选中第一个产品id
//		LoginUserPO loginUser = WebUtil.getLoginUser(request);
//		Set<Integer> productIds = loginUser.getProductIds();
//
//		if(!productIds.isEmpty()){
//			if(productId!=null && productIds.contains(productId)){
//				modelAndView.addObject("redirect", redirect+"?productId="+productId);
//				modelAndView.addObject("productId",productId);
//			}else{
//				List<Integer> productIdList = new ArrayList<>(productIds);
//				Collections.sort(productIdList);
//				modelAndView.addObject("redirect", redirect+"?productId="+productIdList.get(0));
//				 modelAndView.addObject("productId",productIdList.get(0));
//			}
//		}else{
//			modelAndView.addObject("redirect", redirect);
//		}
		
		

		return modelAndView;
	}
	
	@RequestMapping("/page")
	public ModelAndView page(HttpServletRequest request){
		
		ModelAndView modelAndView = new ModelAndView("/haha");


		return modelAndView;
	}
	
}
